package com.xish.api.activity;

import com.mysema.query.sql.SQLQuery;
import com.xish.api.activity.search.query.ActivitySearchSqlQuery;
import com.xish.model.bean.WzmActivity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.query.QueryDslJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.xish.model.table.QWzmActivity.wzmActivity;
import static com.xish.model.table.QWzmActivitySearch.wzmActivitySearch;

@Repository
public class ActivityMysqlDao {

    private static final Logger LOG = LoggerFactory.getLogger(ActivityMysqlDao.class);

    @Autowired
    private QueryDslJdbcTemplate jdbcTemplate;

    public List<Integer> searchIdListByQuery(ActivitySearchSqlQuery query) {
        return jdbcTemplate.query(query.getSQLQuery(), wzmActivitySearch.id);
    }

    public long countByQuery(ActivitySearchSqlQuery query) {
        return jdbcTemplate.count(query.getSQLQuery());
    }

    public WzmActivity findById(int id) {
        SQLQuery query = jdbcTemplate.newSqlQuery()
                .from(wzmActivity).where(wzmActivity.id.eq(id));

        return jdbcTemplate.queryForObject(query, wzmActivity);
    }
}
